package org.outlook.test.ui.effect;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class WindowJitter implements Runnable {
	private Component com;
	private static int times = 5;

	WindowJitter(Component com) {
		this.com = com;
	}

	WindowJitter(Component com, int i) {
		this.com = com;
		if(times>0)
			times  = i;
	}

	@Override
	public void run() {
		Point location = com.getLocation();
		for (int i=10; i > 0; i--)
			for (int j = times; j > 0; j--) {
				com.setLocation((int)location.getX(), (int)location.getY()+i);
				com.setLocation((int)location.getX()+i, (int)location.getY());
				com.setLocation((int)location.getX(), (int)location.getY()-i);
				com.setLocation((int)location.getX()-i, (int)location.getY());
			}
	}

	public static void main(String[] args) {
		final JFrame frame = new JFrame();
		final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setPreferredSize(new Dimension(500,400));
		frame.setLocation((int)(screenSize.getWidth()-500)/2, (int)(screenSize.getHeight()-400)/2);
		frame.setAlwaysOnTop(true);
		frame.setVisible(true);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JFrame control = new JFrame();
		final JButton bt = new JButton("GO");
		bt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new WindowJitter(frame)).start();
			}
		});
		control.add(bt);
		control.setVisible(true);
		control.pack();
		control.setLocation(screenSize.width-100, screenSize.height-80);
	}

}
